package com.gin.no_gay_alliance.fragment.post.emote;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.databinding.FragmentEmoteBinding;
import com.gin.no_gay_alliance.databinding.MergeTabLayoutViewpager2ReverseBinding;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.manager.EmoteManager;
import com.gin.no_gay_alliance.viewmodel.emote.EmoteGroupModel;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 发帖界面，用于添加表情
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/27 19:18
 **/
public class EmoteFragment extends BaseBindingFragment<FragmentEmoteBinding> implements TabLayoutMediator.TabConfigurationStrategy {
    TabLayout tabLayout;
    ViewPager2 vp2;
    TabLayoutMediator tabLayoutMediator;
    private List<EmoteGroupModel> emoteGroups;

    @Override
    protected void onCreateView() {
        MergeTabLayoutViewpager2ReverseBinding bind = MergeTabLayoutViewpager2ReverseBinding.bind(b.getRoot());
        tabLayout = bind.tabLayout;
        vp2 = bind.vp2;
        // 获取表情数据
        EmoteManager emoteManager = MyApplication.app.managerFactory.getEmoteManager();
        // 屏蔽的分组名
        Set<String> bLockedNames = EmoteManager.getBLockedNames();

        // 完整的表情分类
        EmoteManager.getEmoteCategories(false, categories -> {
            // 表情分组(使用屏蔽数据过滤)
            emoteGroups = categories.stream()
                    .flatMap(i -> i.groups.stream())
                    .filter(i -> !bLockedNames.contains(i.name))
                    .collect(Collectors.toList());

            // 添加常用表情分组
            EmoteGroupModel generalEmoteGroup = emoteManager.buildGeneralEmoteGroup(categories);
            if (generalEmoteGroup.emotes != null && !generalEmoteGroup.emotes.isEmpty()) {
                emoteGroups.add(0, generalEmoteGroup);
            }

            // ui操作
            ui(() -> {
                vp2.setAdapter(new FragmentStateAdapter(this) {
                    @NonNull
                    @Override
                    public Fragment createFragment(int position) {
                        return EmoteGroupFragment.newInstance(emoteGroups.get(position));
                    }

                    @Override
                    public int getItemCount() {
                        return emoteGroups.size();
                    }
                });
                // 绑定tabLayout和ViewPager2
                tabLayoutMediator = attachTabLayout(tabLayoutMediator, tabLayout, vp2);
            });
        });
    }


    @Override
    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
        if (emoteGroups == null) {
            return;
        }
        EmoteGroupModel emoteGroup = emoteGroups.get(position);
        if (emoteGroup == null) {
            return;
        }
        // 组名设置为tab的标题
        tab.setText(emoteGroup.name);
    }
}
